package org.ansel.webnodb.demo.controller;

import java.io.File;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

@Controller
@RequestMapping({"/hfs"})
public class HfsController {
    private static final Logger LOGGER = LoggerFactory.getLogger(HfsController.class);
    @RequestMapping({"/"})
    public View index() {
        return new RedirectView("filemgr");
    }
    @RequestMapping({"/filemgr"})
    public String pagename(Model m) {
        File file1 = null;
        file1 = new File(String.valueOf(System.getProperty("user.dir")) + "/mystatic/");
        String[] fnames = file1.list();
        m.addAttribute("fnames", fnames);
        return "filemgr";
    }

    @PostMapping({"/uploadfile"})
    public View uploadfile(@RequestParam("dir") MultipartFile[] dir, @RequestParam("file") MultipartFile[] file) {
        byte b;
        int i;
        MultipartFile[] arrayOfMultipartFile;
        for (i = (arrayOfMultipartFile = dir).length, b = 0; b < i; ) {
            MultipartFile f = arrayOfMultipartFile[b];
            String filename = f.getOriginalFilename();
            if (!"".equals(filename)) {
                File file1 = null;
                file1 = new File(String.valueOf(System.getProperty("user.dir")) + "/mystatic/" + filename);
                file1.mkdirs();
                try {
                    file1.createNewFile();
                    f.transferTo(file1);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            b++;
        }
        for (i = (arrayOfMultipartFile = file).length, b = 0; b < i; ) {
            MultipartFile f = arrayOfMultipartFile[b];
            String filename = f.getOriginalFilename();
            if (!"".equals(filename)) {
                File file1 = null;
                file1 = new File(String.valueOf(System.getProperty("user.dir")) + "/mystatic/" + filename);
                file1.mkdirs();
                try {
                    file1.createNewFile();
                    f.transferTo(file1);
                } catch (IOException e) {
                    LOGGER.error("upload file error.", e);
                    throw new RuntimeException(e);
                }
            }
            b++;
        }
        return new RedirectView("filemgr");
    }
}
